﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bigger.Framework;
using Bigger.Base;
using Bigger.Base.Cache;
using Bigger.Study.Business;
using Bigger.Study.DataEntity;
using Bigger.Web.Mvc;
using Bigger.Web.Helper;
using Bigger.Core.Service;
using Bigger.Core.Logging;
using System.Threading;

namespace Bigger.Study.Web.Controllers
{
    public class HomeController : StudyController
    {
        public ActionResult ThreadSleepTest()
        {
            DateTime startTime = DateTime.Now;
            DateTime endTime = startTime.AddSeconds(10);
            while (true)
            {
                Thread.Sleep(10000);
            }
            return Content("HomeController.ThreadSleepTest");
        }

        public ActionResult Index()
        {
            return View();
        }

        [ChildActionOnly]
        public ActionResult Child1()
        {
            return View();
        }

        [ChildActionOnly]
        public ActionResult Child2()
        {
            string str = string.Format("{0}");
            return View();
        }

        [ChildActionOnly]
        public ActionResult Child3()
        {
            return View();
        }

        public ActionResult Demo(int? id)
        {
            if (!id.HasValue)
            {
                //return Http404();
                SystemWebAsset.CreateHttp404Result();
            }
            return View();
        }

        public ActionResult JsonDemo()
        {
            InvokeResult objInvokeResult = new InvokeResult();
            string str = string.Format("{0}");
            objInvokeResult.Data = new
            {
                Name = "杨廷兴",
                Age = 100
            };
            return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
        }

        public ActionResult JsonpDemo()
        {
            InvokeResult objInvokeResult = new InvokeResult();
            objInvokeResult.ResultMessage = "这是一个Jsonp请求";
            objInvokeResult.Data = new
            {
                Name = "杨廷兴",
                Age = 100
            };
            return Jsonp(objInvokeResult, JsonRequestBehavior.AllowGet);
            //return Jsonp(objInvokeResult, JsonRequestBehavior.AllowGet, "www.baidu.com");
        }

        [CorsFilter("http://Bigger.study.com")]
        public ActionResult CorsDemo()
        {
            InvokeResult objInvokeResult = new InvokeResult();
            objInvokeResult.ResultMessage = "这是一个跨域请求";
            objInvokeResult.Data = new
            {
                Name = "杨廷兴",
                Age = 100
            };
            return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ViewPageDemo()
        {
            Tuple<string, string> objTuple = new Tuple<string, string>("Tuple", "TupleExample");
            //ViewBag
            //ViewData
            //HttpContext.Items
            return View(objTuple);
        }

        [Route("home/lualu")]
        //[OverrideActionFilters]
        public ActionResult RouteDemo()
        {
            return View();
        }

        [OutputCache(Duration = 10)]
        [Route("home/course-{courseId}")]
        public ActionResult CourseDetail(Guid? courseId)
        {
            //if (!this.IsWxRequest)
            //{
            //    return Http404();
            //    //或者特性
            //}
            if (!courseId.HasValue)
            {
                return Http404();
            }
            BizCourse objBizCourse = new BizCourse();
            Course objCourse = objBizCourse.GetRecordCache("", courseId.Value, TimeSpan.FromSeconds(20));
            if (objCourse == null)
            {
                return Http404();
            }
            return View(objCourse);
        }

        public ActionResult RedirectDemo()
        {
            return Redirect("/");
            return RedirectPermanent("/");
            SystemWebAsset.CreateRedirectResult("/");
            SystemWebAsset.CreateRedirectResult("/", true);
        }

        public ActionResult Access()
        {
            //测试地址路径：/home/access/view-index 访问视图Index
            return CheckView();
        }

        public ActionResult GetService()
        {
            ILogService objLogService = UnityConfiguration.UnityConfig.GetService<ILogService>();
            return Content(string.Format("ILogService的实例是否为空：{0}", objLogService != null ? "否" : "是"));
        }
    }
}
